﻿/**************************************************************
 * Copyright Eastmoney Corporation. All rights reserved.
 * 
 * Author: guitao(guitao@eastmoney.com) 
 * Create Date: 2020/6/8 13:31:49
 * Description:
 *          
 * Revision History:
 *      Date         Author               Description
 *              
***************************************************************/

using BenchmarkDotNet.Attributes;
using Confluent.Kafka;
using System;
using System.Collections.Generic;
using System.Text;

namespace BenchMarking.Sample
{
    public class KafkaProducerContext
    {
        private readonly IProducer<string, string> _producer;

        public KafkaProducerContext()
        {
            _producer = new ProducerBuilder<string, string>(new ProducerConfig
            {
                BootstrapServers = "10.228.129.151:9092,10.228.129.152:9092,10.228.129.153:9092",
                RequestTimeoutMs = 3000,
                MessageTimeoutMs = 10000,
                SecurityProtocol = SecurityProtocol.SaslPlaintext,
                SaslMechanism = SaslMechanism.Plain,
                QueueBufferingMaxMessages = 1000000,
                SaslUsername = "kafka",
                SaslPassword = "kafka#secret"
            }).Build();
            //_producer = new ProducerBuilder<string, string>(new ProducerConfig
            //{
            //    BootstrapServers = "10.205.107.31:9092,10.205.112.93:9092,10.205.108.24:9092",
            //    RequestTimeoutMs = 3000,
            //    MessageTimeoutMs = 10000,
            //    QueueBufferingMaxMessages = 1000000,
            //    SecurityProtocol = SecurityProtocol.SaslPlaintext,
            //    SaslMechanism = SaslMechanism.Plain,
            //    SaslUsername = "passport",
            //    SaslPassword = "!0ka$!uhukFCDB88"
            //}).Build();
        }

        [Benchmark]
        public void Publish()
        {
            _producer.Produce("myTopic", new Message<string, string> { Value = Guid.NewGuid().ToString() });
        }

        [Benchmark]
        public void PublishAsync()
        {
            _producer.ProduceAsync("myTopic", new Message<string, string> { Value = Guid.NewGuid().ToString() });
        }
    }
}
